package channelService

import (
	"gitee.com/fierce_wolf/go-fox-edge-channel-common/channelLogger"
	"gitee.com/fierce_wolf/go-fox-edge-channel-common/channelStatus"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisRpc"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Hex"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Reflect"
	"go-fox-edge-channel-mqtt-lora/channelEntity"
	"go-fox-edge-channel-mqtt-lora/channelEvent"
)

type reportService struct {
}

func (e *reportService) popAll(channelType string) ([]*commRedisRpc.ChannelRespondVO, error) {
	respondVOList := make([]*commRedisRpc.ChannelRespondVO, 0)

	for channelEvent.List.HasMessage() {
		data := channelEvent.List.Wait(1000)
		devEUI := Map.GetString(data, "devEUI", "")

		val, ok := service.deviceEUI2Entity.Load(devEUI)
		if !ok {
			continue
		}
		entity := val.(*channelEntity.LoraWanEntity)

		recv := data["data"]
		if recv != nil && Reflect.IsBytes(recv) {
			recv = Hex.EncodeToString(recv.([]byte))
		}

		respondVO := commRedisRpc.Type.ChannelRespondVO.New()
		respondVO.Type = channelType
		respondVO.Name = entity.ChannelName
		respondVO.Recv = recv

		respondVOList = append(respondVOList, respondVO)

		// 更新接收状态
		channelStatus.Traffic.ActiveTrafficUploadPack(entity.ChannelName)

		channelLogger.PrintChannelDebugLogger(respondVO.Name, "上报", respondVO.Recv)
	}

	return respondVOList, nil

}
